<style>
table.lo * { font-size: 12px; font-family: Trebuchet MS; }
table.lo { width: 100%; }
table.lo td { vertical-align: top; text-align: left; }
table.lo td.col { width: 300px; }
.pure_form_extra { width: 100%; }
.pure_form_extra label.file { text-align: left !important; }
.pure_form_extra label.file i { margin-left: 5px !important; margin-top: 3px !important; cursor: pointer; }
.pure_form_extra label.file span.file { display: inline-block !important; background: #fff; border: 1px solid #ddd; width: 120px; padding: 5px 10px; }

</style>

<?php 
$data = $orm->get('pb_colg_template', NULL, NULL, NULL, NULL, NULL)->d;
if (count($data) == 0) {
  $obj = new stdClass();
  $orm->add('pb_colg_template', $obj);
  $obj->id = $orm->last_id();
  $data = array($obj);
}
?>

<table class="lo" cellPadding="0" cellSpacing="0">
  <tr>
    <td class="col">
      <div class="pure-form pure-form-aligned pure_form_extra">
        <legend><b>Attendance & Warning letter</b></legend>
        
        <div class="pure-control-group">
          <label>Attendance Sheet<span id="c_attendance_ul"></span></label>
          <label class="file" id="c_attendance"></label>
        </div>
        
        <div class="pure-control-group">
          <label>Verbal warning letter<span id="c_verbal_ul"></span></label>
          <label class="file" id="c_verbal"></label>
        </div>
        
        <div class="pure-control-group">
          <label>1st warning letter<span id="c_1st_ul"></span></label>
          <label class="file" id="c_1st"></label>
        </div>
        
        <div class="pure-control-group">
          <label>2nd warning letter<span id="c_2nd_ul"></span></label>
          <label class="file" id="c_2nd"></label>
        </div>
      </div>
      
      <div class="pure-form pure-form-aligned pure_form_extra">
        <legend><b>Finance</b></legend>
        
        <div class="pure-control-group">
          <label>Invoice<span id="f_inv_ul"></span></label>
          <label class="file" id="f_inv"></label>
        </div>
      </div>
    </td>
    <td class="col">
      <div class="pure-form pure-form-aligned pure_form_extra">
        <legend><b>Diploma</b></legend>
        
        <div class="pure-control-group">
          <label>Offer<span id="d_offer_ul"></span></label>
          <label class="file" id="d_offer"></label>
        </div>
        
        <div class="pure-control-group">
          <label>Confirmation of Place<span id="d_cop_ul"></span></label>
          <label class="file" id="d_cop"></label>
        </div>
        
        <div class="pure-control-group">
          <label>Academic Transcript<span id="d_transcript_ul"></span></label>
          <label class="file" id="d_transcript"></label>
        </div>
      </div>
      
      <div class="pure-form pure-form-aligned pure_form_extra">
        <legend><b>Language</b></legend>
        
        <div class="pure-control-group">
          <label>Offer<span id="l_offer_ul"></span></label>
          <label class="file" id="l_offer"></label>
        </div>
        
        <div class="pure-control-group">
          <label>Confirmation of Place<span id="l_cop_ul"></span></label>
          <label class="file" id="l_cop"></label>
        </div>
      </div>
      
      <div class="pure-form pure-form-aligned pure_form_extra">
        <legend><b>NCEA</b></legend>
        
        <div class="pure-control-group">
          <label>Offer<span id="n_offer_ul"></span></label>
          <label class="file" id="n_offer"></label>
        </div>
        
        <div class="pure-control-group">
          <label>Confirmation of Place<span id="n_cop_ul"></span></label>
          <label class="file" id="n_cop"></label>
        </div>
      </div>
    </td>
  </tr>
</table>

<script>
  /*<![CDATA[*/
  var __data = JSON.parse('<?php echo addslashes(json_encode($data)); ?>');
  var __dir = '<?php echo PLGN_DIR . basename(dirname(__FILE__)); ?>';
  var __dlurl = '<?php echo Utils::get_dlurl($app_data); ?>';
  var __requrl = '<?php echo Utils::get_requrl($app_data); ?>';
  var __file = '<?php echo addslashes($app_data->param_file); ?>';
  var __redmd = '<?php echo addslashes($app_data->sys->redmd); ?>';
  var __sys = '<?php echo addslashes($app_data->sys->php_name); ?>';
  var __id = '<?php echo $app_data->param_id; ?>';
  var __session_id = '<?php echo session_id(); ?>';
  
  $(function() {
    set_ul('c_attendance');
    set_ul('c_verbal');
    set_ul('c_1st');
    set_ul('c_2nd');
    set_ul('f_inv');
    set_ul('d_offer');
    set_ul('d_cop');
    set_ul('d_transcript');
    set_ul('l_offer');
    set_ul('l_cop');
    set_ul('n_offer');
    set_ul('n_cop');
    
    render();
  });

  function render() {
    set_file('c_attendance');
    set_file('c_verbal');
    set_file('c_1st');
    set_file('c_2nd');
    set_file('f_inv');
    set_file('d_offer');
    set_file('d_cop');
    set_file('d_transcript');
    set_file('l_offer');
    set_file('l_cop');
    set_file('n_offer');
    set_file('n_cop');
  };

  function set_file(elm) {
    $('#' + elm).empty();
    if (__data[0][elm] != '') {
      $('#' + elm).html(
        '<span class="file" title="' + __data[0][elm + '_n'] + '">' +
          Utils.get_short(__data[0][elm + '_n'], 8) +
          '<i title="Remove" class="fa fa-times" style="float: right" onclick="delfile(\'' + elm + '\')"></i>' + 
          '<i title="Download" class="fa fa-download" style="float: right" onclick="dlfile(\'' + elm + '\')"></i>' + 
        '</span>'
      );
    }
  };

  function delfile(elm) {
    Ajax.request(
      __requrl + 'remove',
      'req=' + Utils.encodeURI(elm),
      function(rslt) {
        __data[0][elm] = '';
        __data[0][elm + '_n'] = '';
        render();
      },
      { confirm: 1 }
    );
  };

  function dlfile(elm) {
    window.open(__dlurl + '&p=' + __data[0][elm] + '&n=' + __data[0][elm + '_n']);
  };

  function set_ul(elm) {
    Upload.init(
      elm + '_ul', 
      __requrl + 'upload', 
      function(rslt, srv_err) {
        __data[0][elm] = rslt[0][elm];
        __data[0][elm + '_n'] = rslt[0][elm + '_n'];
        render();
      }, 
      {
        file_types: '*.docx;*.xlsx;*.doc;*.xls',
        file_types_description: 'Template Files',
        file_queue_limit: 1,
        file_size_limit: '5MB',
        upload_start_handler: function(file) {
          this.setPostParams({
            PHPSESSID: __session_id,
            id: __data[0].id,
            item: elm
          });
        },
        debug: 0
      }
    );
  };
  /*]]>*/
</script>